from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2
import numpy as np


# 用户增加
@index_bp.get('/linear_pro')
@index_bp.post('/linear_pro')
@view_logging_required
@permission_required("linear:pro")
def linear_pro():
    if request.method == "GET":
        return render_template('admin/cv/linear.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            file_url = file_read(f)
            in_image = cv2.imread(file_url)
            a = float(request.form.get('degree'))
            out_image = float(a) * in_image
            # 进行数据截断, 大于255的值要截断为255
            out_image[out_image > 255] = 255
            # 数据类型转化
            out_image = np.round(out_image)
            out_image = out_image.astype(np.uint8)
            res_pic = "static/download/gama.jpg"
            cv2.imwrite(res_pic, out_image)
            return render_template('admin/cv/linear.html', pic=file_url, pic_res=res_pic)
        else:
            return "请传入正确的文件格式"